package com.cskaoyan.controller;

import com.cskaoyan.bean.*;
import com.cskaoyan.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @USER: cuiweiguo#
 * @DATE: 2022/5/8
 * @TIME: 10:25
 * @PROJECT_NAME: mallproject2
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    OrderService orderService;

    @RequestMapping("list")
    public AdminListRespVo<WxOrderListBean> list(Integer showType, Integer page, Integer limit) {
        //Request URL: http://182.92.235.201:8083/wx/order/list?showType=0&page=1&limit=10
        //Request Method: GET
        //TODO:两行代码获得session中的iuserId
        Subject subject = SecurityUtils.getSubject();
        Integer id = (Integer) subject.getPrincipals().getPrimaryPrincipal();

        //数据库查询
        WxOrderListBean wxOrderListBean = orderService.wxlist(showType, page, limit, id);

        return AdminListRespVo.listOk(wxOrderListBean);
    }

    @GetMapping("detail")
    public AdminListRespVo<WxOrderDetailBean> detail(Integer orderId) {
        //Request URL: http://182.92.235.201:8083/wx/order/detail?orderId=96
        //Request Method: GET
        //数据库查询
        WxOrderDetailBean wxOrderDetailBean = orderService.wxOrderDetail(orderId);

        return AdminListRespVo.listOk(wxOrderDetailBean);
    }

    @PostMapping("cancel")
    public AdminListRespVo<WxOrderDetailBean> cancel(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        //Request URL: http://182.92.235.201:8083/wx/order/cancel
        //Request Method: POST
        //数据库查询
        int code = orderService.wxCancelOrder(orderId);
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return null;//TODO:后续处理
    }

    @PostMapping("delete")
    public AdminListRespVo<WxOrderDetailBean> delete(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        //Request URL: http://182.92.235.201:8083/wx/order/delete
        //Request Method: POST
        //数据库查询
        int code = orderService.wxDeleteOrder(orderId);
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return null;//TODO:后续处理
    }

    @PostMapping("confirm")
    public AdminListRespVo<WxOrderDetailBean> confirm(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        //Request URL: http://182.92.235.201:8083/wx/order/delete
        //Request Method: POST
        //数据库查询
        int code = orderService.wxConfirmOrder(orderId);
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return null;//TODO:后续处理
    }

    @PostMapping("refund")
    public AdminListRespVo<WxOrderDetailBean> refund(@RequestBody Map map) {
        Integer orderId = (Integer) map.get("orderId");
        //Request URL: http://182.92.235.201:8083/wx/order/delete
        //Request Method: POST
        //数据库查询
        int code = orderService.wxRefundOrder(orderId);
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return null;//TODO:后续处理
    }

    @PostMapping("prepay")
    public AdminListRespVo<WxOrderDetailBean> prepay(@RequestBody Map map) {
        String id = (String) map.get("orderId");
        int orderId = Integer.parseInt(id);
        //Request URL: http://localhost:8083/wx/order/prepay
        //Request Method: POST
        //数据库查询
        int code = orderService.wxPrepayOrder(orderId);//逻辑付款
        if (code == 200) {
            return AdminListRespVo.listOk("null");
        }
        return null;//TODO:后续处理
    }

    @GetMapping("goods")
    public AdminListRespVo<WxOrderGoods> goods(Integer orderId, Integer goodsId) {
        //查询订单商品的详情
        WxOrderGoods wxOrderGoods = orderService.wxGoods(orderId, goodsId);

        return AdminListRespVo.listOk(wxOrderGoods);

    }

    @PostMapping("comment")
    public AdminListRespVo<WxOrderGoods> comment(@RequestBody WxOrderParamBean wxOrderParamBean) {

        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipals().getPrimaryPrincipal();

        //插入评论
        int code = orderService.comment(wxOrderParamBean,userId);
        if (code==200){
            return AdminListRespVo.listOk("null");
        }
        return null;

    }
}
